<?php

$this->addTitle($lang['admin']['base_title']." - ".$lang['admin']['group_title']);
echo "<h3>".$lang['admin']['group_title']."</h3><br />";
echo "\n";
$data_saved=false;
$data_deleted=false;

if(isset($_GET['delete']) && is_numeric($_GET['delete'])){
	$this->sql->queryS("DELETE FROM\n\t`".$this->sql->getpre()."group`\nWHERE\n\t`group_id`=".$_GET['delete'].";");
	if($this->sql->error==""){
		$data_deleted=true;
	}
}

if(isset($_POST['group_save'])){
	foreach($_POST as $post=>$value){
		$part = explode("_",$post);
		if(isset($part[0], $part[1]) && is_numeric($part[1]) && $part[0]!="sql"){
			if(!isset($active[$part[1]])) $active[$part[1]]=0;
			switch($part[0]){
				case "name": $part[0]="group_title"; break;
				case "key": $part[0]="group_key"; break;
				case "info": $part[0]="group_info"; break;
			}
			if(!isset($data_save[$part[1]])){
				$data_save[$part[1]][0]="UPDATE\n\t`".$this->sql->getpre()."group`\nSET\n\t`".$part[0]."`=\"".$value."\"";
				$data_save[$part[1]][1]="\nWHERE\n\t`group_id`=".$part[1].";";
			}else
				$data_save[$part[1]][0].=",\n\t`".$part[0]."`=\"".$value."\"";
		}
	}
	
	if(isset($_POST['sql_new']) && $_POST['sql_new']==1){
		$data_save['new'][0]="INSERT INTO\n\t`".$this->sql->getpre()."group`\nSET\n\t`group_title`=\"".VARsql($_POST['name_new'])."\", \n\t`group_key`=\"".VARsql($_POST['key_new'])."\", \n\t`group_info`=\"".VARsql($_POST['info_new'])."\";";
		$data_save['new'][1]="";
	}
	
	foreach($data_save as $key=>$data_query){
		if(isset($_POST['sql_'.$key]) && $_POST['sql_'.$key]==1)
			$this->sql->queryS($data_query[0].$data_query[1]);
	}
	if($this->sql->error=="")
		$data_saved=true;
	
	$data_deleted=false;
}

$sort_w="";
$sort="";
if(isset($_GET['sort'])){
	$sort_w="\nORDER BY\n\t";
	switch($_GET['sort']){
		case 1: if(isset($_GET['desc']) && $_GET['desc']=="true") $sort_w.="group_title DESC"; else $sort_w.="group_title"; break;
		case 2: if(isset($_GET['desc']) && $_GET['desc']=="true") $sort_w.="group_key DESC"; else $sort_w.="group_key"; break;
	}
	$sort=$_GET['sort'];
	unset($_GET['sort']);
	if(isset($_GET['desc'])){
		unset($_GET['desc']);
		$sort="";
	}
}
$this->sql->query("SELECT \n\tg.* \nFROM \n\t`".$this->sql->getpre()."group` g".$sort_w);

?>
<script language="javascript">
update=0;

function change(sql_update){
	document.getElementById('data_changed').style.display="";
	document.getElementById('data_saved').style.display="none";
	document.getElementById('data_deleted').style.display="none";
	document.getElementById(sql_update).value=1;
	update=1;
}
</script>
<form id="form_0" name="form1" method="post" action="<?php unset($_GET['delete']); echo fullURL(); ?>">
<div align="center">
	<div id="data_changed" style="display:none; font-weight:bold; font-size:12px; color:#000000; width:50%; text-align:center; padding:4px; margin-bottom:8px; background:#E14517; border-style:solid; border-width:3px; border-color:#990000; filter:alpha(opacity=60); -moz-opacity: 0.6; opacity: 0.6;"><?php echo $lang['admin']['info_nsave']; ?></div>
	<div id="data_saved" style=" <?php if(isset($data_saved) && !$data_saved) { ?>display:none;<?php }; ?> font-weight:bold; font-size:12px; color:#000000; width:50%; text-align:center; padding:4px; margin-bottom:8px; background:#009900; border-style:solid; border-width:3px; border-color:#003300; filter:alpha(opacity=60); -moz-opacity: 0.6; opacity: 0.6;"><?php echo $lang['admin']['info_saved']; ?></div>
	<div id="data_deleted" style=" <?php if(isset($data_deleted) && !$data_deleted) { ?>display:none;<?php }; ?> font-weight:bold; font-size:12px; color:#000000; width:50%; text-align:center; padding:4px; margin-bottom:8px; background:#E14517; border-style:solid; border-width:3px; border-color:#990000; filter:alpha(opacity=60); -moz-opacity: 0.6; opacity: 0.6;"><?php echo $lang['admin']['info_deleted']; ?></div>
</div>
<table width="100%" border="0" cellpadding="3" cellspacing="0">
  <tr>
    <th width="25" align="center">&nbsp;</th>
    <th width="200" align="left"><a href="<?php echo fullURL();?>&sort=1<?php if($sort==1) echo "&desc=true"; ?>" onclick="if(update==1){ return confirm('<?php echo $lang['admin']['sort_confirm']; ?>') }"><?php echo $lang['admin']['name']; ?></a></th>
    <th align="center" width="50"><a href="<?php echo fullURL();?>&sort=2<?php if($sort==2) echo "&desc=true"; ?>" onclick="if(update==1){ return confirm('<?php echo $lang['admin']['sort_confirm']; ?>') }">ID</a></th>
    <th align="center"><strong><?php echo $lang['admin']['description']; ?></strong></th>
    <th width="100" align="center">&nbsp;</th>
  </tr>
<?php 
$c=0;
while($this->sql->fetch_assoc()){ 
?>
  <tr id="id_<?php echo $this->sql->row['group_id']; ?>_0" style=" <?php if($c%2){ ?>background-image:url(images/list_gray.png); filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='images/list_gray.png', sizingMethod='scale');<?php }; ?>">
    <td width="25" align="center"><strong><?php echo $c+1; ?></strong></td>
    <td><?php echo $this->sql->row['group_title']; ?></td>
    <td align="center"><?php echo $this->sql->row['group_key']; ?></td>
    <td><?php echo $this->sql->row['group_info']; ?></td>
    <td width="100"><a href="<?php echo fullURL();?>&delete=<?php echo $this->sql->row['group_id']; ?>" onclick="return confirm('<?php echo $lang['admin']['del_confirm']; ?>')"><img src="images/icon_del.png" alt="<?php echo $lang['admin']['link_delete']; ?>" title="<?php echo $lang['admin']['link_delete']; ?>" width="16" height="16" border="0" /></a> <a onclick="hide('id_<?php echo $this->sql->row['group_id']; ?>_0');hide('id_<?php echo $this->sql->row['group_id']; ?>_1');"><img src="images/icon_edit.png" alt="<?php echo $lang['admin']['link_edit']; ?>" title="<?php echo $lang['admin']['link_edit']; ?>" width="16" height="16" /></a></td>
  </tr>
  <tr id="id_<?php echo $this->sql->row['group_id']; ?>_1" style="display:none; <?php if($c%2){ ?>background-image:url(images/list_gray.png); filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='images/list_gray.png', sizingMethod='scale');<?php }; ?>">
    <td width="25" align="center"><strong><?php echo $c+1; ?></strong></td>
    <td>
      <input name="name_<?php echo $this->sql->row['group_id']; ?>" type="text" value="<?php echo $this->sql->row['group_title']; ?>" onChange="change('sql_<?php echo $this->sql->row['group_id']; ?>')" />
      <input id="sql_<?php echo $this->sql->row['group_id']; ?>" name="sql_<?php echo $this->sql->row['group_id']; ?>" type="hidden" value="0" />    </td>
    <td align="center"><input style="text-align:center" name="key_<?php echo $this->sql->row['group_id']; ?>" type="text" size="4" maxlength="4" onChange="change('sql_<?php echo $this->sql->row['group_id']; ?>')" value="<?php echo $this->sql->row['group_key']; ?>" /></td>
    <td>
      <textarea name="info_<?php echo $this->sql->row['group_id']; ?>" id="info_id" style="width:100%" onChange="change('sql_<?php echo $this->sql->row['group_id']; ?>')"><?php echo $this->sql->row['group_info']; ?></textarea>    </td>
    <td width="100"><a onclick="hide('id_<?php echo $this->sql->row['group_id']; ?>_0');hide('id_<?php echo $this->sql->row['group_id']; ?>_1');"><img src="images/icon_ok.png" alt="<?php echo $lang['admin']['link_close']; ?>" title="<?php echo $lang['admin']['link_close']; ?>" width="16" height="16" /></a></td>
  </tr>
<?php 
$c++;
};
?>
  <tr id="new" style="display:none; <?php if($this->browser!="ie" && $c%2){ ?>background-image:url(images/list_gray.png); <?php }elseif($c%2){ ?>filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='images/list_gray.png', sizingMethod='scale');<?php }; ?>">
    <td width="25" align="center"><strong>NEW</strong></td>
    <td>
      <input name="name_new" type="text" onChange="change('sql_new')" /><input id="sql_new" name="sql_new" type="hidden" value="0" /></td>
    <td align="center"><input style="text-align:center" name="key_new" type="text" size="4" maxlength="4" onChange="change('sql_new')" /></td>
    <td>
      <textarea name="info_new" id="info_id" style="width:100%" onChange="change('sql_new')"></textarea>    </td>
    <td width="100"><a onclick="hide('id_<?php echo $this->sql->row['group_id']; ?>_0');hide('id_<?php echo $this->sql->row['group_id']; ?>_1');"></a></td>
  </tr>
</table>
<a id="new_link" onclick="hide('new');hide('new_link');change('sql_new');"><br /><img src="images/icon_add.png" alt="<?php echo $lang['admin']['link_new']; ?>" title="<?php echo $lang['admin']['link_new']; ?>" width="16" height="16" /></a>
<br />
<br />

<input type="submit" name="group_save" value="<?php echo $lang['admin']['button_save']; ?>" />

</form>